package com.example.sanchuang_demo.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.sanchuang_demo.domain.Membership;
import com.example.sanchuang_demo.mapper.MembershipMapper;
import com.example.sanchuang_demo.web.dto.RequestParam;
import com.example.sanchuang_demo.web.dto.ResponseDto;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/sc_demo/membership")
@Slf4j
public class MembershipController {
    @Resource

    private MembershipMapper membershipMapper;
    @GetMapping("/test")
    public String test(){
        return  "hello world";

    }
    @CrossOrigin
    @PostMapping("/list")
    public ResponseDto<?> list(@RequestBody RequestParam param){
        log.info("1");
        if(param.pageNum==null || param.pageSize==null){
            return new ResponseDto<>(false,"缺少pagenum或者pagesize字段",null);
        }
        PageHelper.startPage(param.pageNum, param.pageSize);
        List<Membership> membership= membershipMapper.getMembership();
        PageInfo<Membership> pageInfo = new PageInfo<>(membership);
        return new ResponseDto<>(true,null, pageInfo);

    }
    @PostMapping("/add")
    public ResponseDto<?> add(@RequestBody Membership membership){
        if (!isValidMembership(membership)) return new ResponseDto<>(false, "必填项不能为空！", null);
        if(membership.getSequential()<1||membership.getSequential()>10) return new ResponseDto<>(false, "排序必须是1-10之间！", null);
        membershipMapper.insert(membership);
        return new ResponseDto<>(true,null,"添加成功！");
    }

    private boolean isValidMembership(@RequestBody Membership membership) {
        List<Object> fieldsToCheck = Arrays.asList(
                membership.getName(),
                membership.getRegionId(),
                membership.getMobile(),
                membership.getEmail(),
                membership.getStudentId(),
                membership.getCategory(),
                membership.getSequential()
        );

        return fieldsToCheck.stream()
                .allMatch(field -> field != null && !(field instanceof String && ((String) field).isEmpty()));
    }

    @GetMapping("/delete")
    public ResponseDto<?> delete(@org.springframework.web.bind.annotation.RequestParam("id") String id){
        if(id==null)
            return new ResponseDto<>(false,"学号字段不存在！",null);
        membershipMapper.deleteById(id);
        return new ResponseDto<>(true,null,"删除成功！");
    }
    @PostMapping("/modify")
    public ResponseDto<?> modify(@RequestBody Membership membership){
        if (!isValidMembership(membership)) return new ResponseDto<>(false, "必填项不能为空！", null);
        if(membership.getSequential()<1||membership.getSequential()>10) return new ResponseDto<>(false, "排序必须是1-10之间！", null);
        membershipMapper.updateById(membership);
        return new ResponseDto<>(true,null,"修改成功！");
    }
}
